package com.tzh.plugin.ex;

import com.tzh.plugin.utils.ControllerUtils;
import com.tzh.plugin.utils.GenerateUtils;
import org.mybatis.generator.api.GeneratedJavaFile;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RestGeneratePlguin extends PluginAdapter {

    @Override
    public boolean validate(List<String> warnings) {
        return true;
    }


    @Override
    public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles(IntrospectedTable introspectedTable) {
        String classType = introspectedTable.getBaseRecordType();
        String[] pathname = classType.split("\\.");
        String className = pathname[(pathname.length - 1)];
        String packagePth = classType.replace(".model." + className, "");

        char[] nameChars = className.toCharArray();
        nameChars[0] += 32;
        String classObjName = String.copyValueOf(nameChars);
        String restPath = packagePth + ".controller";
        String restName = className + "Controller";
        String restFullName = restPath + "." + restName;

        TopLevelClass cls = new TopLevelClass(new FullyQualifiedJavaType(restFullName));
        String tableComment = introspectedTable.getTableConfiguration().getProperties().get(introspectedTable.getFullyQualifiedTableNameAtRuntime() + ":comments").toString();
        cls.addAnnotation("@Api(value=\"" + (StringUtils.isEmpty(tableComment) ? introspectedTable.getFullyQualifiedTableNameAtRuntime() : tableComment) + "相关接口\",tags= {\"" + (StringUtils.isEmpty(tableComment) ? introspectedTable.getFullyQualifiedTableNameAtRuntime() : tableComment) + "增删改查操作\"})");
        ControllerUtils.addRestAnnotation(cls, classObjName);
        ControllerUtils.addRestImportTypes(cls);
        Map<String, FullyQualifiedJavaType> dtoData = new HashMap<String, FullyQualifiedJavaType>();
        GenerateUtils.setImports(cls, packagePth + ".dto", className, classType, dtoData);
        ControllerUtils.addField(cls, dtoData, className);
        ControllerUtils.addMethods(cls, introspectedTable, dtoData);
        cls.setVisibility(JavaVisibility.PUBLIC);
        cls.setAbstract(false);
        List<GeneratedJavaFile> generatedJavaFiles = new ArrayList<GeneratedJavaFile>();
        GeneratedJavaFile gxf = null;
        try {
            gxf = new GeneratedJavaFile(cls, context.getJavaModelGeneratorConfiguration().getTargetProject(), context.getJavaFormatter());
        } catch (Exception e) {
            e.printStackTrace();
        }
        generatedJavaFiles.add(gxf);
        System.out.println(context.getJavaFormatter().getFormattedContent(cls));
        return generatedJavaFiles;
    }


}
